package coreservlets.ch15;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowBalance extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String customerId = req.getParameter("id");
		BankCustomer customer = BankCustomer.getCustomer(customerId);
		String address;
		if (customer == null) {
			address = "/WEB-INF/bank-account/UnknownCustomer.jsp";
		} else if (customer.getBalance() < 0) {
			address = "/WEB-INF/bank-account/NegativeBalance.jsp";
			req.setAttribute("badCustomer", customer);
		} else if (customer.getBalance() < 10000) {
			address = "/WEB-INF/bank-account/NormalBalance.jsp";
			req.setAttribute("regularCustomer", customer);
		} else {
			address = "/WEB-INF/bank-account/HighBalance.jsp";
			req.setAttribute("eliteCustomer", customer);
		}
		RequestDispatcher dispatcher = req.getRequestDispatcher(address);
		dispatcher.forward(req, resp);
	}
}
